
#Confirmatory Factor Analysis--Differentiating PICS and place resentment from other attitudinal measures
# B. Kal Munis, PhD Candidate, Department of Politics, University of Virginia

#Replication Code for: Us Over Here Versus Them Over There...Literally: Measuring Place Resentment in American Politics

setwd("C:/Users/kalmu/OneDrive/Dissertation/Data/PICS Chapter 3")

library(lavaan)
library(readstata13)
library(psych)
library(GPArotation)

pics<-read.dta13("replication_data_1.dta")

#######################################################################################
#######################################################################################
#confirmatory factor analysis
#######################################################################################
#######################################################################################

################################
#CFA for full 10 item measure

model1<- 'placeres =~ cultural_1 + cultural_2 + cultural_3 + cultural_4 + representational_3 + distributional_1 + distributional_2 + distributional_3 + distributional_4 + representational_4
        ideopolar =~ ideo_polarization_1 + ideo_polarization_2 + ideo_polarization_3 + ideo_polarization_4 + ideo_polarization_5 
        popsov =~ pop_sov_1 + pop_sov_2 + pop_sov_3 + pop_sov_4
        popanti =~ pop_anti_1 + pop_anti_2 + pop_anti_3 + pop_anti_4 
        pophom =~ pop_hom_1 + pop_hom_2 + pop_hom_3 
        racialres =~ rr_1 + rr_2 + rr_3 + rr_4'

#resultsrural<-cfa(model, data=subset(pics, rural==1))
results1<-cfa(model1, data=pics, std.lv=TRUE, missing="fiml")

summary(results1, fit.measures=TRUE) #distinguishable from the other constructs; yay!
# representational, cultural, and distributional aspects of consciousness are merely facets rather than distinct attitudinal dimensions
parameterEstimates(results1, standardized=TRUE)

################################
#CFA to determine if distinguishable from other place constructs.
modelplace<- 'placeres =~ cultural_1 + cultural_2 + cultural_3 + cultural_4 + representational_3 + distributional_1 + distributional_2 + distributional_3 + distributional_4 + representational_4
         attachment =~ attachment_1 + attachment_2+ attachment_3+ attachment_4+ attachment_5+ attachment_6
identity =~ identity_1 + identity_2 + identity_3 + identity_4 + identity_5 '

resultsplace<-cfa(modelplace, data=pics,  std.lv=TRUE, missing="fiml")

summary(resultsplace, fit.measures=TRUE) #distinguishable from the other PLACE constructs; yay!

################################
#CFA for truncated 4 item model 
modelkmnj<- 'kmnj =~ distributional_2 + distributional_3 + distributional_4 + representational_4
         ideopolar =~ ideo_polarization_1 + ideo_polarization_2 + ideo_polarization_3 + ideo_polarization_4 + ideo_polarization_5 
popsov =~ pop_sov_1 + pop_sov_2 + pop_sov_3 + pop_sov_4
popanti =~ pop_anti_1 + pop_anti_2 + pop_anti_3 + pop_anti_4 
pophom =~ pop_hom_1 + pop_hom_2 + pop_hom_3 
racialres =~ rr_1 + rr_2 + rr_3 + rr_4'

resultskmnj<-cfa(modelkmnj, data=pics, std.lv=TRUE, missing="fiml")

summary(resultskmnj, fit.measures=TRUE) #distinguishable from the other constructs; yay!
